package com.zhongjian.technologycal.mall.activity.personal.fragment;

import android.os.Bundle;
import android.widget.ListView;

import com.google.gson.Gson;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.zhongjian.technologycal.mall.R;
import com.zhongjian.technologycal.mall.adapter.WaitDeliveryOrderAdapter;
import com.zhongjian.technologycal.mall.base.BaseFragment;
import com.zhongjian.technologycal.mall.bean.OrderBean;
import com.zhongjian.technologycal.mall.interfacemall.OrderInterface;
import com.zhongjian.technologycal.mall.net.HttpRetrofitClient;
import com.zhongjian.technologycal.mall.utils.LoadingTip;
import com.zhongjian.technologycal.mall.utils.NetWorkUtils;
import com.zhongjian.technologycal.mall.utils.UserUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import butterknife.BindView;
import okhttp3.ResponseBody;
import rx.Subscriber;

/**
 * Created by wei on 2017/12/15.
 * 待收货的订单列表
 */

public class WaitDeliveryOrderListFragment extends BaseFragment {
    public static final String CATEGORY_NAME = "com.nanchen.aiyagirl.module.category.CategoryFragment.CATEGORY_NAME";
    @BindView(R.id.delivery_lv_order_listView)
    ListView delivery_lv_order_listView;
    @BindView(R.id.loadedTip)
    LoadingTip loadingTip;
    @BindView(R.id.delivery_RefreshLayout)
    RefreshLayout refreshLayout;
    private WaitDeliveryOrderAdapter waitDeliveryOrderAdapter;
    private List<OrderBean> listItems = new ArrayList<>();
    private int page = 0;
    private int pageNum =20;

    @Override
    protected void initView() {
//        refreshLayout.autoRefresh();
        initRequest();
        waitDeliveryOrderAdapter = new
                WaitDeliveryOrderAdapter(getActivity(), listItems, new OrderInterface() {
            @Override
            public void isRefreshAll(boolean isSuccess) {
                if(isSuccess){
                    initRequest();
                }
            }
        });
        delivery_lv_order_listView.setAdapter(waitDeliveryOrderAdapter);

        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(final RefreshLayout refreshlayout) {
                refreshlayout.getLayout().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        page = 0;
                        initRequest();
                        refreshlayout.finishRefresh();
                        refreshlayout.setLoadmoreFinished(false);
                    }
                }, 1000);
            }
        });
        refreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(final RefreshLayout refreshlayout) {
                page++;
                refreshlayout.getLayout().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        page++;
                        initRequest();
                        refreshLayout.finishLoadmore();

                    }
                }, 1000);
            }
        });

    }

    @Override
    protected int getLayoutResource() {
        return R.layout.fragment_wait_delivery_list;
    }


    public static WaitDeliveryOrderListFragment newInstance(String mCategoryName) {
        WaitDeliveryOrderListFragment categoryFragment = new WaitDeliveryOrderListFragment();
        Bundle bundle = new Bundle();
        bundle.putString(CATEGORY_NAME, mCategoryName);
        categoryFragment.setArguments(bundle);
        return categoryFragment;
    }

    @Override
    public void initPresenter() {
    }

    private void initRequest() {
        if (!NetWorkUtils.isNetConnected(getActivity())) {
            loadingTip.setLoadingTip(LoadingTip.LoadStatus.error);
            return;
        }
        HttpRetrofitClient.getInstance(getActivity()).getUrlRequestOrderList(UserUtils.getUserToken(),
                "wr",page,pageNum, new Subscriber<ResponseBody>() {
            @Override
            public void onStart() {
                super.onStart();
                loadingTip.setLoadingTip(LoadingTip.LoadStatus.loading);
            }

            @Override
            public void onCompleted() {
                if (listItems.size() <= 0) {
                    loadingTip.setLoadingTip(LoadingTip.LoadStatus.empty);
                }

            }


            @Override
            public void onError(Throwable e) {
                loadingTip.setLoadingTip(LoadingTip.LoadStatus.sereverError);
            }

            @Override
            public void onNext(ResponseBody responseBody) {
                loadingTip.setLoadingTip(LoadingTip.LoadStatus.finish);
                try {
                    Gson gson = new Gson();
                    OrderBean orderBean = gson.fromJson(responseBody.string(), OrderBean.class);
                    List<OrderBean> forList = new ArrayList<>();
                    for (int i = 0; i < orderBean.getData().size(); i++) {
                        forList.add(orderBean);
                    }
                    setData(forList);

                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        });
    }

    private void setData(List<OrderBean> foreshowList) {
        if(page ==0){
            listItems.clear();
        }
        Collections.reverse(listItems);
        for (OrderBean vo : foreshowList) {
            listItems.add(vo);
        }
        waitDeliveryOrderAdapter.notifyDataSetChanged();

    }

    @Override
    public void onResume() {
        super.onResume();
        initRequest();
    }
}
